package com.atguigu.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "公孙胜", "入云龙");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero4);

        doubleLinkedList.list();

        System.out.println("remove...");

        doubleLinkedList.remove(3);
        doubleLinkedList.list();

        System.out.println("update...");

        doubleLinkedList.update(new HeroNode2(2,"卢本伟","五五开"));
        doubleLinkedList.list();
    }
}

class DoubleLinkedList implements LinkedList{
    HeroNode2 head=new HeroNode2(0,"","");

    @Override
    public void list() {
        HeroNode2 temp=head.next;
        if (temp==null) {
            System.out.println("链表为空");
            return;
        }

        while(true){
            if (temp!=null) {
                System.out.println(temp);
                temp=temp.next;
            }else{
                break;
            }
        }
    }

    @Override
    public void add(LinkedListNode node) {
        add((HeroNode2) node);
    }

    @Override
    public void addByOrder(LinkedListNode node) {
        addByOrder((HeroNode2) node);
    }

    public void addByOrder(HeroNode2 node){
        HeroNode2 temp=head;
        while(temp.next!=null){
            if (temp.next.no>node.no) {
                // 修改序号时, 优先修改编外节点, 再修改节点内容的深层次, 再至浅层次
                node.next=temp.next;
                node.prev=temp;
                temp.next.prev=node;
                temp.next=node;
                return;
            }

            temp=temp.next;
        }
        node.prev=temp;
        temp.next=node;
    }

    public void add(HeroNode2 node){
        HeroNode2 temp=head;
        while(true){
            if (temp.next!=null) {
                temp=temp.next;
            }else{
                temp.next=node;
                node.prev=temp;
                break;
            }
        }
    }

    @Override
    public void update(LinkedListNode node) {
        update((HeroNode2) node);
    }

    public void update(HeroNode2 node){
        HeroNode2 temp=head;
        if (temp.next==null) {
            System.out.println("链表为空");
            return;
        }

        while(true){
            if (temp.next==null) {
                System.out.println("未找到匹配的节点");
                return;
            }
            if (temp.next.no==node.no) {
                temp.next.name=node.name;
                temp.next.nickname=node.nickname;
                return;
            }

            temp=temp.next;
        }
    }

    @Override
    public void remove(int no) {
        HeroNode2 temp=head.next;
        while(temp!=null){
            if (temp.no==no) {
                temp.prev.next=temp.next;
                if (temp.next!=null) {
                    temp.next.prev=temp.prev;
                }
                break;
            }

            temp=temp.next;
        }
    }

    @Override
    public int size() {
        HeroNode2 temp=head.next;
        if(temp==null){
            return 0;
        }
        int size=0;
        while(true){
            size++;
            if (temp.next==null) {
                return size;
            }
            temp=temp.next;
        }
    }
}

class HeroNode2 implements LinkedListNode{
    int no;
    String name;
    String nickname;
    HeroNode2 next;
    HeroNode2 prev;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }


    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
